﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZBJF.Warranty.Common.Common;
using ZBJF.Warranty.Model;
using ZBJF.Warranty.Model.ViewModel.Product;
// ReSharper disable All
namespace ZBJF.Warranty.BLL
{
    public partial class ProductModelsService
    {
        public ProductModels LoadByID(long id)
        {
            return LoadEntities(a => a.ID == id).FirstOrDefault();
        }
        public ProductModels LoadByModelCode(string modelCode)
        {
            return LoadEntities(a => a.ModelCode.Equals(modelCode)).FirstOrDefault();
        }
        public ProductModels LoadByName(long brandId, string modelName)
        {
            return LoadEntities(a => a.BrandId == brandId && a.ModelName == modelName).FirstOrDefault();
        }
        public bool IsExist(long brandId, string modelName, long id = 0)
        {
            var expressionWhere = PredicateExtensions.True<ProductModels>();
            if (!string.IsNullOrWhiteSpace(modelName))
                expressionWhere = expressionWhere.And(o => o.ModelName.Equals(modelName) && o.BrandId == brandId);
            if (id != 0)
                expressionWhere = expressionWhere.And(o => o.ID != id);
            return LoadEntities(expressionWhere).Any();
        }


        /// <summary>
        /// 获得所有配件类型的型号
        /// </summary>
        /// <param name="brandId">获得该品牌下所有的配件类型的型号</param>
        /// <returns></returns>
        public IQueryable<ProductModels> GetProductModel(long brandId = -1)
        {
            var expressionWhere = PredicateExtensions.True<ProductModels>();
            if (brandId > 0)
            {
                expressionWhere = expressionWhere.And(o => o.BrandId == brandId);
            }
            expressionWhere = expressionWhere.And(o => o.IsAvailable);

            return LoadEntities(expressionWhere).OrderBy(o => o.Sort);
        }

        public IQueryable<ProductModels> GetProductModel(ProductModelsModel model)
        {
            var expressionWhere = PredicateExtensions.True<ProductModels>();
            expressionWhere = expressionWhere.And(o => o.IsAvailable);
            if (model.BrandId.HasValue && model.BrandId.Value > 0)
            {
                expressionWhere = expressionWhere.And(o => o.BrandId == model.BrandId.Value);
            }
            if (!string.IsNullOrEmpty(model.ModelName))
            {
                expressionWhere = expressionWhere.And(o => o.ModelName.Contains(model.ModelName));
            }
            return LoadEntities(expressionWhere).OrderBy(o => o.Sort);
        }

        public IQueryable<ProductModels> GetPagingList(ProductModelsModel model, out int total)
        {
            var expressionWhere = PredicateExtensions.True<ProductModels>();
            if (!string.IsNullOrEmpty(model.Keyword))
            {
                string kw = model.Keyword.ToLower();
                expressionWhere = expressionWhere.And(o => o.ModelName.ToLower().Contains(kw) || o.ModelName.ToLower().Contains(kw));
            }
            if (!string.IsNullOrEmpty(model.BrandsIDs))
            {
                var expressright =
                                PredicateExtensions.False<ProductModels>();
                var arr = model.BrandsIDs.Split(',');
                foreach (var item in arr)
                {
                    if (string.IsNullOrEmpty(item)) continue;
                    var brandId = 0;
                    if (int.TryParse(item, out brandId))
                    {
                        expressright = expressright.Or(c => c.BrandId == brandId);
                    }
                }
                expressionWhere = expressionWhere.And(expressright);
            }
            if (model.BrandId.HasValue)
            {
                expressionWhere = expressionWhere.And(o => o.BrandId == model.BrandId);
            }
            if (model.ScreenType.HasValue)
            {
                expressionWhere = expressionWhere.And(o => o.ScreenType == model.ScreenType);
            }
            if (model.IsAvailable.HasValue)
            {
                expressionWhere = expressionWhere.And(c => c.IsAvailable == model.IsAvailable);
            }
            if (string.IsNullOrWhiteSpace(model.iSorts))
            {
                model.iSorts = "Sort asc";
            }
            return LoadPagerEntities<ProductModels>(model.PageIndex, model.iDisplayLength, out total, expressionWhere, model.iSorts);
        }

        #region 获取设备信息 + EquipmentInfo GetEquipmentInfo(long modelId)
        /// <summary>
        /// 获取设备信息
        /// </summary>
        /// <param name="modelId">型号ID</param>
        /// <returns></returns>
        public EquipmentInfo GetEquipmentInfo(long modelId)
        {
            return (from a in LoadEntities(t => t.ID == modelId)
                    join b in ServiceFactory.ProductBrandsServiceInstance.LoadEntities(t => true) on a.BrandId equals b.ID
                    join c in ServiceFactory.ProductTypeServiceInstance.LoadEntities(t => true) on b.ProductTypeId equals c.ID
                    select new 
                    {
                        BrandId = b.ID,
                        BrandName = b.BrandName,
                        ModelId = a.ID,
                        ModelName = a.ModelName,
                        ProductTypeId = c.ID,
                        ProductTypeName = c.ProductName
                    }
                ).ToList().Select(t=>new EquipmentInfo {
                        BrandId = t.BrandId,
                        BrandName = t.BrandName,
                        ModelId = t.ModelId,
                        ModelName = t.ModelName,
                        ProductTypeId = t.ProductTypeId,
                        ProductTypeName = t.ProductTypeName
                }).FirstOrDefault();
            
        }
        #endregion

        #region 获取型号名称 + string GetModelName(long productModelId)
        /// <summary>
        /// 获取型号名称
        /// </summary>
        /// <param name="productModelId">型号ID</param>
        /// <returns></returns>
        public string GetModelName(long productModelId)
        {
            var item = LoadByID(productModelId);
            if (item == null)
                return "";
            return item.ModelName;
        }
        #endregion
    }
}
